library(reticulate)

# list available conda envs
conda_list()

# activate a specific env
use_condaenv("scvelo-colorbar")
scv <- import("scvelo")
scv$logging$print_version()
py_run_string('scv.logging.print_version()')

# use built-in dataset
adata = scv$read('/home/gjsx/learn/scvelo/data/Pancreas/endocrinogenesis_day15.h5ad', cache=TRUE)
adata

scv$pl$scatter(adata, legend_loc='lower left', size=60)

## get embedding
emb <- adata$obsm['X_umap']
rownames(emb) <- adata$obs_names$values
emb |> head()

clusters <- adata$obs$clusters
names(clusters) <- adata$obs_names$values
head(clusters)

# TODO plot umap in R

## preprocess
py_run_string('scv.pp.filter_and_normalize(adata, min_shared_counts=20, n_top_genes=2000)')

## run scvelo dynamic model
py_run_string('scv.tl.recover_dynamics(adata, n_jobs = 20)')

py_help(scv$tl$recover_dynamics)

## takes awhile, so uncomment to save
#adata$write('data/pancreas.h5ad', compression='gzip')
#adata = scv$read('data/pancreas.h5ad')